fix(cbq): read_index / MmapReader crash on zero-record CBQ files - #101
Merged
noamteyssier merged 5 commits intoAug 10, 2026
Merged
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds an empty guard in Index::from_bytes to handle zero-record CBQ files, preventing bytemuck::try_cast_slice from failing on empty decompressed buffers due to alignment checks on dangling pointers. It also adds a unit test to verify this behavior for the streaming Reader. The reviewer suggests expanding the test coverage to include MmapReader and optimizing the test by avoiding an unnecessary .to_vec() allocation.
Collaborator
|
@alejandrogzi - Thanks for the submission, will release now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix(cbq): read_index / MmapReader crash on zero-record CBQ files
Summary
Index::from_bytesfails on empty input, so a valid CBQ file containingzero records (e.g. a run where an upstream filter discarded every read) cannot
have its index read: both the streaming
Reader::read_indexandMmapReader::newreturnCbqError::IndexCastingError("Unable to cast bytes to Index - likely an alignment error"). This makes
bqtools decode(and any other index-reading consumer) crash on such files.Root cause
A zero-record CBQ has an empty index:
u_bytes = 0and a 9-byte zstd frame ofzero-length content. After decompression the buffer is an empty
Vec<u8>,whose pointer is dangling and aligned only to 1.
bytemuck::try_cast_slice::<u8, BlockRange>checks pointer alignment even forempty slices, so it returns
TargetAlignmentGreaterAndInputNotAligned.The bytes on disk are canonical — they are exactly what
ColumnarBlockWriterproduces for empty input — so this is purely a reader-side issue.
Fix
Guard the empty case before casting (
src/cbq/core/index.rs):Test
Added
cbq::read::tests::test_read_index_empty_file(
src/cbq/read.rs): writes a zero-record CBQ viaColumnarBlockWriter,reads it back with the streaming
Reader, and asserts the index loads with0 records / 0 blocks.
CbqError(IndexCastingError)— passes with the fix.cargo test(327 tests + doctests),cargo fmt --check,cargo clippy.